﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dotNetInteropPlugin.PluginDev;

namespace ManagedTestPlugin2
{
    public partial class TestForm : Form
    {
        public TestForm()
        {
            InitializeComponent();
            EqFrm = new EqualizerForm();
        }

        EqualizerForm EqFrm = null;

        public IMAIMPPlayer player
        {
            get;set;
        }
        //public SafeEventManager eventMan
        //{
        //    get;set;
        //}

        OptFrameData OptionFrame;

        public void InitForm(IMAIMPPlayer p, string Cap)
        {
            this.Text += " - " + Cap;
            player = p;

            player.EventManager.EffectChangedEvent += new MAIMPPlayerEventsDelegate(AdditionalParams_EffectChangedEvent); // eventMan.GetInvocation(AdditionalParams_EffectChangedEvent);
            player.EventManager.InfoUpdateEvent += new MAIMPPlayerEventsDelegate(AdditionalParams_InfoUpdateEvent); //eventMan.GetInvocation(AdditionalParams_InfoUpdateEvent);
            //player.AdditionalParams.PlayerStateChangedEvent += eventMan.GetInvocation(AdditionalParams_PlayerStateChangedEvent);
            //player.AdditionalParams.PlayFileEvent += eventMan.GetInvocation(AdditionalParams_PlayFileEvent);
            player.EventManager.SatusChangeEvent += new MAIMPPlayerEventsDelegate(AdditionalParams_SatusChangeEvent); //eventMan.GetInvocation(AdditionalParams_SatusChangeEvent);
            player.EventManager.TrackPosChangedEvent += new MAIMPPlayerEventsDelegate(AdditionalParams_TrackPosChangedEvent);  //eventMan.GetInvocation(AdditionalParams_TrackPosChangedEvent);
            player.EventManager.EQChangedEvent += new MAIMPPlayerEventsDelegate(AdditionalParams_EQChangedEvent);  //eventMan.GetInvocation(AdditionalParams_EQChangedEvent);

            lblNowPlaying.Text = "-";
            trbPosition.Value = 0;
            trbPosition.Maximum = 0;
            lblPostition.Text = "00:00:00";


            MAIMPMenuItem mi = new MAIMPMenuItem();
            mi.Text = "none";
            mi.ItemType = AIMPMenuItemType.Normal;
            mi.BmpImage = new Bitmap(this.Icon.ToBitmap(), 16, 16);
           // mi.Click += new EventHandler(mi_Click);

            MAIMPMenuItem miSub = new MAIMPMenuItem();
            miSub.Text = "none";
            miSub.Click += new EventHandler(mi_Click);
            mi.SubMenuItmes.Add(miSub);

            player.MenuManager.Add(AIMPPlayerMenu.AIMP_UTILS_MENU, mi);

            chbRepeate.Checked = player.PlayingOrderParams.IsRepeatTrack;
            chbShuffle.Checked = player.PlayingOrderParams.IsShuffleMode;

            AdditionalParams_InfoUpdateEvent(this, MAIMPPlayerEventArgs.InfoUpdate);

            OptionFrame = new OptFrameData();
            OptionFrame.Initialize(p);

            this.Show();
        }

        void mi_Click(object sender, EventArgs e)
        {
            MessageBox.Show((sender as MAIMPMenuItem).Text);
        }

        public void Deinit()
        {
            player.EventManager.EffectChangedEvent -= new MAIMPPlayerEventsDelegate(AdditionalParams_EffectChangedEvent); // eventMan.GetInvocation(AdditionalParams_EffectChangedEvent);
            player.EventManager.InfoUpdateEvent -= new MAIMPPlayerEventsDelegate(AdditionalParams_InfoUpdateEvent); //eventMan.GetInvocation(AdditionalParams_InfoUpdateEvent);
            //player.AdditionalParams.PlayerStateChangedEvent += eventMan.GetInvocation(AdditionalParams_PlayerStateChangedEvent);
            //player.AdditionalParams.PlayFileEvent += eventMan.GetInvocation(AdditionalParams_PlayFileEvent);
            player.EventManager.SatusChangeEvent -= new MAIMPPlayerEventsDelegate(AdditionalParams_SatusChangeEvent); //eventMan.GetInvocation(AdditionalParams_SatusChangeEvent);
            player.EventManager.TrackPosChangedEvent -= new MAIMPPlayerEventsDelegate(AdditionalParams_TrackPosChangedEvent);  //eventMan.GetInvocation(AdditionalParams_TrackPosChangedEvent);
            player.EventManager.EQChangedEvent -= new MAIMPPlayerEventsDelegate(AdditionalParams_EQChangedEvent);  //eventMan.GetInvocation(AdditionalParams_EQChangedEvent);
        }



        void AdditionalParams_EQChangedEvent(object sender, MAIMPPlayerEventArgs args)
        {
            EqFrm.listBox1.Items.Clear();
            for (int i = 0; i < player.Equalizer.SlidersCount; i++)
                EqFrm.listBox1.Items.Add(player.Equalizer.SlidersValues[i].ToString() + ":  " + player.Equalizer[i].ToString());
        }

        void AdditionalParams_TrackPosChangedEvent(object sender, MAIMPPlayerEventArgs args)
        {
            trbPosition.Value = (int)player.CurrentPlayingInfo.Position.TotalMilliseconds;
            lblPostition.Text = player.CurrentPlayingInfo.Position.ToString();
        }

        void AdditionalParams_SatusChangeEvent(object sender, MAIMPPlayerEventArgs args)
        {
            trbVolume.Value = (int)player.Volume;
        }

        void AdditionalParams_PlayFileEvent(object sender, MAIMPPlayerEventArgs args)
        {
            trbVolume.Value = (int)player.Volume;
        }

        void AdditionalParams_PlayerStateChangedEvent(object sender, MAIMPPlayerEventArgs args)
        {
            trbVolume.Value = (int)player.Volume;
        }

        void AdditionalParams_InfoUpdateEvent(object sender, MAIMPPlayerEventArgs args)
        {
            if (player.PlayingState != AIMPPlayingState.Stoped)
            {
                MAIMPFileInfo curF = player.CurrentPlayingInfo.GetCurrentTrackInfo();
                lblNowPlaying.Text = curF.Artist + " - " + curF.Title + " (" + curF.Duration.ToString() + ")";
                trbPosition.Maximum = (int)player.CurrentPlayingInfo.Length.TotalMilliseconds;
                trbPosition.Value = (int)player.CurrentPlayingInfo.Position.TotalMilliseconds;
                lblPostition.Text = player.CurrentPlayingInfo.Position.ToString();

                player.MenuManager[0].MenuItem.Text = curF.Title;
                player.MenuManager[0].MenuItem.SubMenuItmes[0].Text = curF.Artist;

                pictureBox1.Image = player.CoverArtManager.GetCurrentCoverArt(pictureBox1.Width, pictureBox1.Height);
            }
            else
            {
                lblNowPlaying.Text = "-";
                trbPosition.Value = 0;
                trbPosition.Maximum = 0;
                lblPostition.Text = "00:00:00";

                player.MenuManager[0].MenuItem.Text = "none";
                player.MenuManager[0].MenuItem.SubMenuItmes[0].Text = "none";

                pictureBox1.Image = null;
            }
        }

        void AdditionalParams_EffectChangedEvent(object sender, MAIMPPlayerEventArgs args)
        {
          //  trbVolume.Value = (int)player.Volume;
            trbChanelBalance.Value = player.ChanelBallance;
            trbPlaySpeed.Value = (int)player.Effects.Speed;
        }

        private void trbPosition_Scroll(object sender, EventArgs e)
        {
            player.CurrentPlayingInfo.Position = TimeSpan.FromMilliseconds(trbPosition.Value);
        }

        private void trbVolume_Scroll(object sender, EventArgs e)
        {
            player.Volume = (uint)trbVolume.Value;
        }

        private void trbChanelBalance_Scroll(object sender, EventArgs e)
        {
            player.ChanelBallance = trbChanelBalance.Value;
        }

        private void chbShuffle_CheckedChanged(object sender, EventArgs e)
        {
            player.PlayingOrderParams.IsShuffleMode = chbShuffle.Checked;
        }

        private void chbRepeate_CheckedChanged(object sender, EventArgs e)
        {
            player.PlayingOrderParams.IsRepeatTrack = chbRepeate.Checked;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            player.Play();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            player.Pause();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            player.Stop();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            player.PrevTrack();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            player.NextTrack();
        }

        private void button11_Click(object sender, EventArgs e)
        {
            try
            {
                EqFrm.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private class ContainerTmp
        {
            public ListBox lbx {get;set;}
            public TabPage tbPg {get;set;}
            public IMAIMPPlaylist Playlist {get;set;}
            public MAIMPConnectedPlaylist ConPls {get;set;}
            public MAIMPDisconnectedPlaylist DiscPls {get;set;}
        }

        private void button6_Click(object sender, EventArgs e)
        {
            tabControl1.TabPages.Clear();
            for (int i = 0; i < player.PlaylistManager.Count; i++)
            {
                MAIMPConnectedPlaylist pl = player.PlaylistManager[i];
                TabPage tbPg = new TabPage(pl.PlaylistName);
                ListBox lbx = new ListBox();

                for (int j = 0; j < pl.Count; j++)
                    lbx.Items.Add(pl.GetTrackTitle(j));

                tbPg.Controls.Add(lbx);
                tabControl1.TabPages.Add(tbPg);

                lbx.Size = tbPg.Size;

                lbx.DoubleClick += new EventHandler(lbx_DoubleClick);

                Button RemBtn = new Button();
                RemBtn.Text = "Удалить";
                lbx.Height -= (RemBtn.Height + 2);
                tbPg.Controls.Add(RemBtn);
                RemBtn.Top = lbx.Height + 2;
                RemBtn.Left = 1;

                RemBtn.Click += new EventHandler(RemBtn_Click);

                Button RemFl = new Button();
                RemFl.Text = "Уд. файл";
                tbPg.Controls.Add(RemFl);
                RemFl.Top = lbx.Height + 2;
                RemFl.Left = RemBtn.Right + 2;

                RemFl.Click += new EventHandler(RemFl_Click);

                Button AddFl = new Button();
                AddFl.Text = "Доб. файл";
                tbPg.Controls.Add(AddFl);
                AddFl.Top = lbx.Height + 2;
                AddFl.Left = RemFl.Right + 2;

                AddFl.Click += new EventHandler(AddFl_Click);

                ContainerTmp CntTmp = new ContainerTmp() { ConPls = pl, lbx = lbx, tbPg = tbPg, Playlist = pl };

                RemBtn.Tag = CntTmp;
                RemFl.Tag = CntTmp;
                AddFl.Tag = CntTmp;
                lbx.Tag = CntTmp;
            }

        }

        void lbx_DoubleClick(object sender, EventArgs e)
        {
            ContainerTmp CntTmp = (sender as ListBox).Tag as ContainerTmp;
            if (CntTmp.Playlist.IsConnected && CntTmp.lbx.SelectedIndex >= 0)
                CntTmp.ConPls.PlayTrack(CntTmp.lbx.SelectedIndex);
        }

        void AddFl_Click(object sender, EventArgs e)
        {
            ContainerTmp CntTmp = (sender as Button).Tag as ContainerTmp;
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                MAIMPFileInfo fi = player.AdditionalParams.GetTrackInfoFromFile(ofd.FileName);
                CntTmp.Playlist.Add(fi);
                CntTmp.lbx.Items.Add(fi.ToString());
            }
        }

        void RemFl_Click(object sender, EventArgs e)
        {
            ContainerTmp CntTmp = (sender as Button).Tag as ContainerTmp;
            if (CntTmp.lbx.SelectedIndex >= 0)
            {
                CntTmp.Playlist.RemoveAt(CntTmp.lbx.SelectedIndex);
                CntTmp.lbx.Items.RemoveAt(CntTmp.lbx.SelectedIndex);
            }
        }

        void RemBtn_Click(object sender, EventArgs e)
        {
            ContainerTmp CntTmp = (sender as Button).Tag as ContainerTmp;
            if (CntTmp.Playlist.IsConnected)
                player.PlaylistManager.Remove(CntTmp.ConPls);
            tabControl1.TabPages.Remove(CntTmp.tbPg);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            AskInputBox inpBx = new AskInputBox();
            if (inpBx.ShowDialog(this, "Заголовок плейлиста:", DateTime.Now.ToString()) == DialogResult.OK)
            {
                MAIMPConnectedPlaylist pl = player.PlaylistManager.CreateEmpty(inpBx.Answer, true);
                TabPage tbPg = new TabPage(pl.PlaylistName);
                ListBox lbx = new ListBox();

                for (int j = 0; j < pl.Count; j++)
                    lbx.Items.Add(pl.GetTrackTitle(j));

                tbPg.Controls.Add(lbx);
                tabControl1.TabPages.Add(tbPg);

                lbx.Size = tbPg.Size;

                lbx.DoubleClick += new EventHandler(lbx_DoubleClick);

                Button RemBtn = new Button();
                RemBtn.Text = "Удалить";
                lbx.Height -= (RemBtn.Height + 2);
                tbPg.Controls.Add(RemBtn);
                RemBtn.Top = lbx.Height + 2;
                RemBtn.Left = 1;

                RemBtn.Click += new EventHandler(RemBtn_Click);

                Button RemFl = new Button();
                RemFl.Text = "Уд. файл";
                tbPg.Controls.Add(RemFl);
                RemFl.Top = lbx.Height + 2;
                RemFl.Left = RemBtn.Right + 2;

                RemFl.Click += new EventHandler(RemFl_Click);

                Button AddFl = new Button();
                AddFl.Text = "Доб. файл";
                tbPg.Controls.Add(AddFl);
                AddFl.Top = lbx.Height + 2;
                AddFl.Left = RemFl.Right + 2;

                AddFl.Click += new EventHandler(AddFl_Click);

                ContainerTmp CntTmp = new ContainerTmp() { ConPls = pl, lbx = lbx, tbPg = tbPg, Playlist = pl };

                RemBtn.Tag = CntTmp;
                RemFl.Tag = CntTmp;
                AddFl.Tag = CntTmp;
                lbx.Tag = CntTmp;
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            AskInputBox inpBx = new AskInputBox();
            if (inpBx.ShowDialog(this, "Заголовок плейлиста:", DateTime.Now.ToString()) == DialogResult.OK)
            {
                MAIMPDisconnectedPlaylist pl = new MAIMPDisconnectedPlaylist(inpBx.Answer);
                TabPage tbPg = new TabPage(pl.PlaylistName);
                ListBox lbx = new ListBox();

                for (int j = 0; j < pl.Count; j++)
                    lbx.Items.Add(pl.GetTrackTitle(j));

                tbPg.Controls.Add(lbx);
                tabControl1.TabPages.Add(tbPg);

                lbx.Size = tbPg.Size;

                lbx.DoubleClick += new EventHandler(lbx_DoubleClick);

                Button RemBtn = new Button();
                RemBtn.Text = "Удалить";
                lbx.Height -= (RemBtn.Height + 2);
                tbPg.Controls.Add(RemBtn);
                RemBtn.Top = lbx.Height + 2;
                RemBtn.Left = 1;

                RemBtn.Click += new EventHandler(RemBtn_Click);

                Button RemFl = new Button();
                RemFl.Text = "Уд. файл";
                tbPg.Controls.Add(RemFl);
                RemFl.Top = lbx.Height + 2;
                RemFl.Left = RemBtn.Right + 2;

                RemFl.Click += new EventHandler(RemFl_Click);

                Button AddFl = new Button();
                AddFl.Text = "Доб. файл";
                tbPg.Controls.Add(AddFl);
                AddFl.Top = lbx.Height + 2;
                AddFl.Left = RemFl.Right + 2;

                AddFl.Click += new EventHandler(AddFl_Click);

                Button ConnectBtn = new Button();
                ConnectBtn.Text = "Подключ.";
                tbPg.Controls.Add(ConnectBtn);
                ConnectBtn.Top = lbx.Height + 2;
                ConnectBtn.Left = AddFl.Right + 2;

                ConnectBtn.Click += new EventHandler(ConnectBtn_Click);


                ContainerTmp CntTmp = new ContainerTmp() { DiscPls = pl, lbx = lbx, tbPg = tbPg, Playlist = pl };

                RemBtn.Tag = CntTmp;
                RemFl.Tag = CntTmp;
                AddFl.Tag = CntTmp;
                lbx.Tag = CntTmp;
                ConnectBtn.Tag = CntTmp;
            }
        }

        void ConnectBtn_Click(object sender, EventArgs e)
        {
            ContainerTmp CntTmp1 = (sender as Button).Tag as ContainerTmp;
            tabControl1.TabPages.Remove(CntTmp1.tbPg);
            MAIMPConnectedPlaylist cnPls = player.PlaylistManager.Connect(CntTmp1.DiscPls, true, false, false);


            MAIMPConnectedPlaylist pl = cnPls;
            TabPage tbPg = new TabPage(pl.PlaylistName);
            ListBox lbx = new ListBox();

            for (int j = 0; j < pl.Count; j++)
                lbx.Items.Add(pl.GetTrackTitle(j));

            tbPg.Controls.Add(lbx);
            tabControl1.TabPages.Add(tbPg);

            lbx.Size = tbPg.Size;

            lbx.DoubleClick += new EventHandler(lbx_DoubleClick);

            Button RemBtn = new Button();
            RemBtn.Text = "Удалить";
            lbx.Height -= (RemBtn.Height + 2);
            tbPg.Controls.Add(RemBtn);
            RemBtn.Top = lbx.Height + 2;
            RemBtn.Left = 1;

            RemBtn.Click += new EventHandler(RemBtn_Click);

            Button RemFl = new Button();
            RemFl.Text = "Уд. файл";
            tbPg.Controls.Add(RemFl);
            RemFl.Top = lbx.Height + 2;
            RemFl.Left = RemBtn.Right + 2;

            RemFl.Click += new EventHandler(RemFl_Click);

            Button AddFl = new Button();
            AddFl.Text = "Доб. файл";
            tbPg.Controls.Add(AddFl);
            AddFl.Top = lbx.Height + 2;
            AddFl.Left = RemFl.Right + 2;

            AddFl.Click += new EventHandler(AddFl_Click);

            ContainerTmp CntTmp = new ContainerTmp() { ConPls = pl, lbx = lbx, tbPg = tbPg, Playlist = pl };

            RemBtn.Tag = CntTmp;
            RemFl.Tag = CntTmp;
            AddFl.Tag = CntTmp;
            lbx.Tag = CntTmp;
        }

        private void TestForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            LanguageTestForm ttt = new LanguageTestForm();
            ttt.Initialize(player);
            ttt.Show();
        }

        private void button10_Click(object sender, EventArgs e)
        {
            ConfigTestForm ttt = new ConfigTestForm();
            ttt.Initialize(player);
            ttt.Show();
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            player.Effects.Speed = (uint)trbPlaySpeed.Value;
        }



    }
}
